package hr.ctg.service.dao;

import hr.ctg.domain.model.Korisnik;
import hr.jpa.dao.GenericDAOImpl;

import javax.persistence.NoResultException;

import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;

@AutoCreate
@Name("korisnikDAO")
public class KorisnikDAO extends GenericDAOImpl<Korisnik, Integer> {

	public Korisnik findByUsernameAktivan(final String username) {

		try {
			return (Korisnik) entityManager.createNamedQuery(
					"Korisnik.findByUsernameAktivan").setParameter(1, username)
					.setParameter(2, true).getSingleResult();
		} catch (final NoResultException e) {
			return null;
		}
	}

	public Long findBrojKorisnika() {
		return (Long) entityManager.createNamedQuery(
				"Korisnik.getKorisnikCount").getSingleResult();
	}

	public void update(final Korisnik korisnik) {
		entityManager.merge(korisnik);
	}

}
